package com.eso.haBank.service.log

import com.eso.frame.utils.HttpUtils
import com.eso.frame.utils.DateUtils

class AccessLogService {

    /**
     * 控制台滚动日志
     * @param request
     * @return
     *
     * @author:RongWei
     * @date:2010-11-10
     */
    def consoleLogAction(request) {
        StringBuilder buff = new StringBuilder()

        buff.append("OPTIME:${URLEncoder.encode(DateUtils.getNow() + "", "utf-8")}")
        buff.append(" REQUEST_METHOD:${URLEncoder.encode(formatStr(request.getMethod()) + "", "utf-8")}")
        buff.append(" REQUEST_URL:${URLEncoder.encode(formatStr(HttpUtils.getPageURL(request)) + "", "utf-8")}")
        buff.append(" USER_AGENT:${URLEncoder.encode(formatStr(request.getHeader("SysUser-Agent")) + "", "utf-8")}")
        buff.append(" STATUS_CODE:${URLEncoder.encode("200", "utf-8")}")//错误,需要修改!!!
        buff.append(" IP_ADDRESS:${URLEncoder.encode(formatStr(HttpUtils.getIpAddr(request)) + "", "utf-8")}")

        buff.append(" REFERER:${URLEncoder.encode(formatStr(request.getHeader("Referer")) + "", "utf-8")}")
        buff.append(" HTTP_CLIENT_IP:${URLEncoder.encode(formatStr(request.getHeader("HTTP_CLIENT_IP")) + "", "utf-8")}")  // 代理端ip（有可能存在，可伪造）
        buff.append(" SERVER_IP_ADDRESS:${URLEncoder.encode(formatStr(request.getLocalAddr()) + "", "utf-8")}")
        buff.append(" TIME_ZONE:${URLEncoder.encode("+0800", "utf-8")}")

        //log.info(buff.toString())
    }

    /**
     * 将空字符串格式哈
     */
    def formatStr(str) {
        if (!str)  str = "-";

        return str
    }

}
